package com.hcbd.tdwt.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileUtil {
    /** 最外层目录 **/
    public final static String SYS_TEMMD = "tdwt";

    /** 缓存目录 **/
    public final static String CACHE = "cache";

    /** 缓存LOG目录 **/
    public final static String LOGS = "logs";

    /** 缓存图片文件目录 **/
    public final static String IMG = "img";

    /** 缓存图片文件目录 **/
    public final static String TEMPORARY = "temporary";

    /** 缓存音频文件目录 **/
    public final static String audio = "audio";
    /** 缓存音频文件目录 **/
    public final static String PCM = "pcm";
    /** 缓存音频文件目录 **/
    public final static String ACC = "acc";
    /** 发送图片存储路径 **/
    public final static String SEND_IMG="sendimg";

    /**nomedia隐藏图片文件不被找到**/
    public final static String NOMEDIA = ".nomedia";
    /**
     * 日志文件
     *
     * @return
     */
    public static String getLogFile() {
        StringBuilder strBuf=new StringBuilder();
        String sdcard = Environment.getExternalStorageDirectory().toString();
        strBuf.append(sdcard);
        strBuf.append("/");
        strBuf.append(SYS_TEMMD);
        File file = new File(strBuf.toString());
        if (!file.exists()) {
            file.mkdir();
        }
        strBuf.append("/");
        strBuf.append(CACHE);
        File file2 = new File(strBuf.toString());
        if (!file2.exists()) {
            file2.mkdir();
        }
        strBuf.append("/");
        strBuf.append(LOGS);
        File file3 = new File(strBuf.toString());
        if (!file3.exists()) {
            file3.mkdir();
        }
        strBuf.append("/");
        return strBuf.toString();
    }
    /**
     * 得到缓存音频文件目录
     *
     * @return
     */
    public static String getAudioAccFile() {
        String sdcard = Environment.getExternalStorageDirectory().toString();
        File file = new File(sdcard + "/" + SYS_TEMMD);
        if (!file.exists())
            file.mkdir();
        File file2 = new File(sdcard + "/" + SYS_TEMMD + "/"+ audio);
        if (!file2.exists())
            file2.mkdir();
        File file3 = new File(sdcard + "/" + SYS_TEMMD + "/"+ audio+"/"
                + ACC);
        if (!file3.exists())
            file3.mkdir();
        return sdcard + "/" + SYS_TEMMD +"/" + audio+ "/"+ACC+"/";
    }

    /**
     * 得到缓存音频文件目录
     *
     * @return
     */
    public static String getAudioPcmFile() {
        String sdcard = Environment.getExternalStorageDirectory().toString();
        File file = new File(sdcard + "/" + SYS_TEMMD);
        if (!file.exists())
            file.mkdir();
        File file2 = new File(sdcard + "/" + SYS_TEMMD + "/"+ audio);
        if (!file2.exists())
            file2.mkdir();
        File file3 = new File(sdcard + "/" + SYS_TEMMD + "/"+ audio+"/"
                + PCM);
        if (!file3.exists())
            file3.mkdir();
        return sdcard + "/" + SYS_TEMMD +"/" + audio+ "/"+PCM+"/";
    }

    /**
     * 得到缓存图片文件目录
     * path
     * @return
     */
    public static String getImgFile(String folderName) {
        StringBuffer strBuf=new StringBuffer();
        strBuf.append(Environment.getExternalStorageDirectory().toString());
        strBuf.append("/");
        strBuf.append(SYS_TEMMD);
        File file = new File(strBuf.toString());
        if (!file.exists()) {
            file.mkdir();
        }
        strBuf.append("/");
        strBuf.append(IMG);
        File file2 = new File(strBuf.toString());
        if (!file2.exists()) {
            file2.mkdir();
        }
        strBuf.append("/");
        strBuf.append(SEND_IMG);
        File file3 = new File(strBuf.toString());
        if (!file3.exists()) {
            file3.mkdir();
        }
        strBuf.append("/");
        strBuf.append(folderName);
        File file4 = new File(strBuf.toString());
        if (!file4.exists()) {
            file4.mkdir();
        }
        createNomediaFile(strBuf.toString());
        strBuf.append("/");
        return strBuf.toString();
    }
    /**
     * 得到缓存图片文件目录
     * path
     * @return
     */
    public static String getReceiveImgFile() {
        StringBuffer strBuf=new StringBuffer();
        strBuf.append(Environment.getExternalStorageDirectory().toString());
        strBuf.append("/");
        strBuf.append(SYS_TEMMD);
        File file = new File(strBuf.toString());
        if (!file.exists()) {
            file.mkdir();
        }
        strBuf.append("/");
        strBuf.append(IMG);
        File file2 = new File(strBuf.toString());
        if (!file2.exists()) {
            file2.mkdir();
        }
        strBuf.append("/");
        strBuf.append(SEND_IMG);
        File file3 = new File(strBuf.toString());
        if (!file3.exists()) {
            file3.mkdir();
        }
        createNomediaFile(strBuf.toString());
        strBuf.append("/");
        return strBuf.toString();
    }
    /**
     * 得到缓存发送图片文件目录
     * path
     * @return
     */
    public static String getSendImgFile() {
        StringBuffer strBuf=new StringBuffer();
        strBuf.append(Environment.getExternalStorageDirectory().toString());
        strBuf.append("/");
        strBuf.append(SYS_TEMMD);
        File file = new File(strBuf.toString());
        if (!file.exists()) {
            file.mkdir();
        }
        strBuf.append("/");
        strBuf.append(IMG);
        File file2 = new File(strBuf.toString());
        if (!file2.exists()) {
            file2.mkdir();
        }
        strBuf.append("/");
        strBuf.append(SEND_IMG);
        File file3 = new File(strBuf.toString());
        if (!file3.exists()) {
            file3.mkdir();
        }
        createNomediaFile(strBuf.toString());
        strBuf.append("/");

        return strBuf.toString();
    }
    /**
     * 创建隐藏文件夹
     * @param file
     */
    public static void createNomediaFile(String file){
        String nodediaPath = file+ "/.nomedia";
        File nomediaFile = new File(nodediaPath);
        if (!nomediaFile.exists()){
            try {
                nomediaFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 得到缓存图片目录
     *
     * @return
     */
    public static String getCacheImage() {
        String sdcard = Environment.getExternalStorageDirectory().toString();
        File file = new File(sdcard + "/" + SYS_TEMMD);
        if (!file.exists())
            file.mkdir();
        File file2 = new File(sdcard + "/" + SYS_TEMMD + "/" + CACHE);
        if (!file2.exists())
            file2.mkdir();
        File file3 = new File(sdcard + "/" + SYS_TEMMD + "/" + CACHE + "/"
                + IMG);
        if (!file3.exists())
            file3.mkdir();
        createNomediaFile(sdcard + "/" + SYS_TEMMD + "/" + CACHE + "/" + IMG);
        return sdcard + "/" + SYS_TEMMD + "/" + CACHE + "/" + IMG + "/";
    }

    public static String getTemporaryPictureFilesJPG() {
        StringBuffer stringBuffer = new StringBuffer(getCacheImage());
        stringBuffer.append(DateUtils.getDateSerial()).append("_temp.jpg");
        return new String(stringBuffer);
    }
    public static String getPictureFilesJPG() {
        StringBuffer stringBuffer = new StringBuffer(getCacheImage());
        stringBuffer.append(DateUtils.getDateSerial()).append("_temp.jpg");
        return new String(stringBuffer);
    }
    /**
     * Uri转File
     *
     * @param uri
     * @param context
     * @return
     */
    public static File getFileByUri(Uri uri, Context context) {
        String path = null;
        if ("file".equals(uri.getScheme())) {
            path = uri.getEncodedPath();
            if (path != null) {
                path = Uri.decode(path);
                ContentResolver cr = context.getContentResolver();
                StringBuffer buff = new StringBuffer();
                buff.append("(").append(MediaStore.Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")");
                Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA}, buff.toString(), null, null);
                int index = 0;
                int dataIdx = 0;
                for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
                    index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
                    index = cur.getInt(index);
                    dataIdx = cur.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    path = cur.getString(dataIdx);
                }
                cur.close();
                if (index == 0) {
                } else {
                    Uri u = Uri.parse("content://media/external/images/media/" + index);
                    //Log.i("InfoMessage", "temp uri is :" + u);
                }
            }
            if (path != null) {
                return new File(path);
            }
        } else if ("content".equals(uri.getScheme())) {
            // 4.2.2以后
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
            if (cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                path = cursor.getString(columnIndex);
            }
            cursor.close();

            return new File(path);
        } else {
            //Log.i(TAG, "Uri Scheme:" + uri.getScheme());
        }
        return null;
    }
    /**
     * 文件转byte
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] InputStream2ByteArray(File file) throws IOException {

        InputStream in = new FileInputStream(file);
        byte[] data = toByteArray(in);
        in.close();
        return data;
    }
    public static byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }   /**
     * 得到key文件
     *
     * @return
     */
    public static String getCacheKeyFile() {
        String sdcard;
        sdcard = Environment.getExternalStorageDirectory().toString();
        File file = new File(sdcard + "/" + ".SC");
        if (!file.exists())
            file.mkdir();
        File file2 = new File(sdcard + "/" + ".SC" + "/" + "cache");
        if (!file2.exists())
            file2.mkdir();
        File file3 = new File(sdcard + "/" + ".SC" + "/" + "cache" + "/" + "key");
        if (!file3.exists())
            file3.mkdir();
        createNomediaFile(sdcard + "/" + ".SC" + "/" + "cache" + "/" + "key");
        return sdcard + "/" + ".SC" + "/" + "cache" + "/" + "key"+"/";
    }
    public  static String ReadTxtFile(String strFilePath) {
        String path = strFilePath;
        String content = ""; //文件内容字符串
        //打开文件
        File file = new File(path);
        InputStream instream =null;
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory()&&!file.exists()) {
            Log.e("TestFile", "The File doesn't not exist.");
        } else {
            try {
                instream = new FileInputStream(file);
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line;
                //分行读取
                while (( line = buffreader.readLine()) != null) {
                    content += line;
                }
            } catch (java.io.FileNotFoundException e) {
                Log.e("TestFile", "The File doesn't not exist.");
            } catch (IOException e) {
                Log.e("TestFile", e.getMessage());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if(instream!=null) {
                        instream.close();
                    }
                }catch (Exception e){
                    // e.printStackTrace();
                }
            }
        }
        return content;
    }
}
